package com.smartao.base.department;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.ForeignKey;

import com.smartao.base.employee.Employee;
import com.smartao.base.selectitem.Selectitem;
import com.smartao.base.tree.TreeNode;
import com.smartao.core.orm.entity.BaseEntity;

/**
 * 部门实体类
 * @author sunxing
 */
@Entity
@Table(name="T_BASE_DEPARTMENT")
public class Department extends BaseEntity<Department> {

	private static final long serialVersionUID = 4641854598267158106L;

	private String name;

	private String code;

	// 上级部门
	@ManyToOne
	@ForeignKey(name = "none")
	@JoinColumn(name = "parent", nullable = true)
	@Fetch(FetchMode.JOIN)
	private Department parent;

	@ManyToOne
	@ForeignKey(name = "none")
	@JoinColumn(name = "manager")
	@Fetch(FetchMode.JOIN)
	private Employee manager;
	
	private Integer dsporder = 0;

	// 左右值保存树形结构
	private Integer leftKey;
	private Integer rightKey;

	// 组织维度
	@ManyToOne
	@ForeignKey(name = "none")
	@JoinColumn(name = "dimension")
	@Fetch(FetchMode.JOIN)
	private Selectitem dimension;

	// 描述
	private String description;
	
	@Transient
	private String path;

	public boolean isLeaf() {
		return (leftKey + 1 == rightKey);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Department getParent() {
		return parent;
	}

	public void setParent(Department parent) {
		this.parent = parent;
	}

	public Employee getManager() {
		return manager;
	}

	public void setManager(Employee manager) {
		this.manager = manager;
	}

	public Integer getDsporder() {
		return dsporder;
	}

	public void setDsporder(Integer dsporder) {
		this.dsporder = dsporder;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	
	public Integer getLeftKey() {
		return leftKey;
	}

	public void setLeftKey(Integer leftKey) {
		this.leftKey = leftKey;
	}

	public Integer getRightKey() {
		return rightKey;
	}

	public void setRightKey(Integer rightKey) {
		this.rightKey = rightKey;
	}

	public Selectitem getDimension() {
		return dimension;
	}

	public void setDimension(Selectitem dimension) {
		this.dimension = dimension;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
	//------------------以下为扩展方法-----------------------------------
	public Integer getChildrenCount() {
		return (rightKey - leftKey - 1) / 2;
	}
	
	public TreeNode toTreeNode(){
		TreeNode node = new TreeNode();
		node.setId(String.valueOf(this.id));
		node.setName(this.name);
		node.setUrl("");
		return node;
	}
	
	@Transient
	public boolean getIsParent(){
		return !isLeaf();
	}
	
	@Override
	public String toString() {
		return name;
	}
	
}
